﻿Imports System
Imports System.IO
Imports System.Data.SqlServerCe
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.Common
Imports System.Reflection

Public Class frmEngineerCoupon

    Sub initScreen()
        cboEngineerCouponStatus.Text = "Pending"

        pnlCouponConfirm.Visible = False
        refreshComboCouponCreateDate(cboEngineerCouponDate)
        refreshComboCouponNo(cboEngineerCouponNo)

        loadEngineerCouponScreen()

        engineerCouponScreen_default()
    End Sub

    Sub engineerCouponScreen_default()
        btnEngineerCouponApprove.Enabled = False
        btnEngineerCouponEdit.Enabled = False
    End Sub

    Private Sub frmEngineerCoupon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadRejectReason(cboCouponConfirmReason, Nothing)
    End Sub

    Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
        _FROM_SCREEN_ID = _SCREEN_ID
        _SCREEN_ID = SCREEN_PROJECT

        frmProject.Show()
        frmProject.initScreen()
        Me.Hide()
    End Sub

    Private Sub btnCouponConfirmApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCouponConfirmApprove.Click
        Dim returnExecute As Boolean = False
        Dim rejectReason As String = ""
        Dim statusCode As String = ""

        If (cboCouponConfirmStatus.Text = "Pending") Then
            statusCode = Globals.COUPON_STATUS_PENDING
        ElseIf (cboCouponConfirmStatus.Text = "Approve") Then
            statusCode = Globals.COUPON_STATUS_APPROVED

            Dim dtResult As New DataTable
            Dim paramList As New Hashtable()
            Dim lstView As New ListViewItem

            dtResult = getCoupon(_lblSelectedCouponId.Text)
            If dtResult.Rows.Count > 0 Then
                Dim dataRow As DataRow = dtResult.Rows(0)

                If (dataRow.Item("COUPON_TYPE") = COUPON_TYPE_OUT) Then
                    statusCode = Globals.COUPON_STATUS_COMPLETED
                End If
            End If
        ElseIf (cboCouponConfirmStatus.Text = "Reject") Then
            statusCode = Globals.COUPON_STATUS_REJECTED
            rejectReason = cboCouponConfirmReason.Text

            If (rejectReason = "") Then
                MsgBox("Reject Reason is empty.", MsgBoxStyle.Information, "EQAM")
                Return
            End If
        ElseIf (cboCouponConfirmStatus.Text = "Complete") Then
            statusCode = Globals.COUPON_STATUS_COMPLETED
        End If

        returnExecute = updateCouponStatus(txtCouponConfirmRemark.Text, rejectReason, statusCode, lblSelectedCouponId.Text)

        If returnExecute = True Then
            MsgBox(_SAVE_SUCCESSFUL_MESSAGE, MsgBoxStyle.Information, "EQAM")
            initScreen()
        Else
            'MessageBox.Show("Error!")
            Return
        End If

    End Sub

    Private Sub btnCouponConfirmReject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCouponConfirmReject.Click
        Dim returnExecute As Boolean = False

        returnExecute = updateCouponStatus(txtCouponConfirmRemark.Text, cboCouponConfirmReason.Text, Globals.COUPON_STATUS_REJECTED, lblSelectedCouponId.Text)

        If returnExecute = True Then
            MsgBox(_SAVE_SUCCESSFUL_MESSAGE, MsgBoxStyle.Information, "EQAM")
            initScreen()
        Else
            'MessageBox.Show("Error!")
            Return
        End If
    End Sub

    Private Sub lvEngineerCouponList_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lvEngineerCouponList.SelectedIndexChanged
        Dim sic As ListView.SelectedIndexCollection
        sic = lvEngineerCouponList.SelectedIndices()

        If sic.Count <> 0 Then
            Dim statusCode As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(3).Text
            If (Globals.COUPON_STATUS_APPROVED = statusCode) Then
                btnEngineerCouponApprove.Enabled = True
                btnEngineerCouponEdit.Enabled = True
            ElseIf (Globals.COUPON_STATUS_PENDING = statusCode) Then
                btnEngineerCouponApprove.Enabled = True
                btnEngineerCouponEdit.Enabled = True
                lblSelectedCouponId.Text = lvEngineerCouponList.Items(sic.Item(0)).Tag
            ElseIf (Globals.COUPON_STATUS_COMPLETED = statusCode) Then
                btnEngineerCouponApprove.Enabled = False
                btnEngineerCouponEdit.Enabled = True
            ElseIf (Globals.COUPON_STATUS_REJECTED = statusCode) Then
                btnEngineerCouponApprove.Enabled = True
                btnEngineerCouponEdit.Enabled = True
            End If
        End If
    End Sub

    Private Sub btnEngineerCouponApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEngineerCouponApprove.Click
        pnlCouponConfirm.Visible = True
        btnEngineerCouponApprove.Enabled = False
        btnEngineerCouponEdit.Enabled = False

        pnlCouponConfirm.Location = New System.Drawing.Point(6, 37)
        pnlCouponConfirm.Size = New System.Drawing.Size(216, 293)

        Dim sic As ListView.SelectedIndexCollection
        sic = lvEngineerCouponList.SelectedIndices()

        cboCouponConfirmStatus.Text = "Approve"
        txtCouponConfirmRemark.Text = ""
        cboCouponConfirmReason.Enabled = False

        If sic.Count <> 0 Then
            Dim coupon As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(0).Text
            Dim typeData As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(2).Text
            Dim reason As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(7).Text
            Dim ar As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(3).Text
            Dim remark As String = lvEngineerCouponList.Items(sic.Item(0)).SubItems(8).Text

            If (ar = vbNullString) Then
                ar = Globals.COUPON_STATUS_APPROVED
            End If

            cboCouponConfirmReason.Text = reason
            cboCouponConfirmStatus.Text = ar
            txtCouponConfirmRemark.Text = remark

            If (ar = Globals.COUPON_STATUS_REJECTED) Then
                cboCouponConfirmReason.Enabled = True
            Else
                cboCouponConfirmReason.Enabled = False
            End If

            lblSelectedCouponId.Text = lvEngineerCouponList.Items(sic.Item(0)).Tag
            txtCouponConfirmCoupon.Text = coupon
            txtCouponConfirmTypeData.Text = typeData
        End If
    End Sub

    Private Sub btnCouponConfirmClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCouponConfirmClose.Click
        pnlCouponConfirm.Visible = False
        btnEngineerCouponApprove.Enabled = True
        btnEngineerCouponEdit.Enabled = True
    End Sub

    Private Sub btnEngineerCouponSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEngineerCouponSearch.Click
        loadEngineerCouponScreen()
    End Sub

    Private Sub loadEngineerCouponScreen()
        Dim status As String = ""
        If (cboEngineerCouponStatus.Text = "Pending") Then
            status = Globals.COUPON_STATUS_PENDING
        ElseIf (cboEngineerCouponStatus.Text = "Approve") Then
            status = Globals.COUPON_STATUS_APPROVED
        ElseIf (cboEngineerCouponStatus.Text = "Reject") Then
            status = Globals.COUPON_STATUS_REJECTED
        ElseIf (cboEngineerCouponStatus.Text = "Complete") Then
            status = Globals.COUPON_STATUS_COMPLETED
        End If
        findCouponForListing(cboEngineerCouponDate.Text, cboEngineerCouponNo.Text, status, txtEngineerCouponBarcode.Text, cboEngineerCouponIO.Text, lvEngineerCouponList)
    End Sub

    Private Sub btnEngineerCouponEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEngineerCouponEdit.Click
        Dim sic As ListView.SelectedIndexCollection
        sic = lvEngineerCouponList.SelectedIndices()
        Dim selectedCouponId As String = ""

        If sic.Count <> 0 Then
            selectedCouponId = lvEngineerCouponList.Items(sic.Item(0)).Tag
            _ACTION = Globals.BUTTON_LABEL_EDIT

            frmCreateCoupon.Show()
            frmCreateCoupon.initScreen()
            frmCreateCoupon.editCoupon(selectedCouponId)
        End If
    End Sub

    Private Sub cboCouponConfirmStatus_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCouponConfirmStatus.SelectedIndexChanged
        If ("Reject" = cboCouponConfirmStatus.Text) Then
            cboCouponConfirmReason.Enabled = True
        Else
            cboCouponConfirmReason.Enabled = False
            cboCouponConfirmReason.Text = ""
        End If
    End Sub

    Private Sub MenuItemMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemMenu.Click
        frmMenu.Show()
        frmMenu.initScreen()
        Me.Hide()
    End Sub
End Class